package com.power.sifa.approve.model.doc;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.plf.core.base.model.EnumerationHelper;
import com.power.sifa.approve.model.Apl_admreview;
import com.power.sifa.approve.model.Apl_agent;
import com.power.sifa.approve.model.Apl_applicant;
import com.power.sifa.approve.model.Apl_main;
import com.power.sifa.approve.model.enumeration.Tp_applicant;
import com.power.sifa.approve.model.enumeration.Tp_procmaterial;
import com.power.sifa.approve.service.Apl_admreviewService;
import com.power.sifa.approve.service.Apl_agentService;
import com.power.sifa.approve.service.Apl_applicantService;
import com.power.sifa.approve.service.Apl_mainService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @ClassName AdmreAuditNoacceptDecisionDoc
 * @Author xiongzw
 * @Date 2023/11/23 16:44
 * @Description 不予受理行政复议申请决定书
 * @Version: 1.0
 **/

@Data
public class AdmreAuditNoacceptDecisionDoc extends AbstractDoc {

    /**
     * 内容
     */
    private String content;

    /**
     * 陈述
     */
    private String statement;


    /**
     * 代理人
     */
    public List<Apl_agent> agentList;


    private final static String TEMPLATE = "    申请人对{apl_admre_audit_targetname}[{behavior}]{prcode}不服，于{apl_main_subtime}向本府提出行政复议申请。经审查，本府认为：（不予受理的事实和理由）。根据《中华人民共和国行政复议法》第十七条第一款的规定，决定不予受理。";

    private final static String STATEMENT_TEMPLATE = "    申请人如果不服本决定，可以自接到本决定书之日起15日内依法向xxx人民法院提起行政诉讼。";



    public AdmreAuditNoacceptDecisionDoc() {
        super();
        this.setDocType(Tp_procmaterial.ADMRE_NOACCEPT);
    }

    public AdmreAuditNoacceptDecisionDoc(String applyid) {
        super(applyid);
        this.setDocType(Tp_procmaterial.ADMRE_NOACCEPT);
    }

    public AdmreAuditNoacceptDecisionDoc(String applyid, String content, String statement) {
        super(applyid);
        this.setDocType(Tp_procmaterial.ADMRE_NOACCEPT);
        this.content = content;
        this.statement = statement;
    }


    @Override
    public void initDocParam (String applyid) {
        Apl_admreviewService apl_admreviewService = SpringUtil.getBean(Apl_admreviewService.class);
        Apl_mainService apl_mainService =  SpringUtil.getBean(Apl_mainService.class);
        Apl_applicantService apl_applicantService =  SpringUtil.getBean(Apl_applicantService.class);
        Apl_agentService apl_agentService = SpringUtil.getBean(Apl_agentService.class);

        Apl_admreview admreview = apl_admreviewService.getById(applyid);
        Apl_main apl_main = apl_mainService.getById(applyid);
        List<Apl_applicant> applicantList = apl_applicantService.getApplicantByApplyid(applyid);

        this.setAdmreObj(admreview);
        this.setApl_admre_audit_code(apl_main.getAcceptcode());
        this.setFaMain(apl_main);
        this.setApplicantList(applicantList);

        List<Apl_agent> agentList  = apl_agentService.getAgentByApplyid(applyid);
        this.setAgentList(agentList);
    }


    @Override
    public AbstractDoc generateTemplate(String applyid) {
        AdmreAuditAcceptNoticeDoc template = new AdmreAuditAcceptNoticeDoc(applyid);
        // 案号
        template.setApl_admre_audit_code(this.getApl_admre_audit_code());
        template.setApl_admre_audit_applyname(ObjectUtil.isNotNull(this.getAdmreObj()) ? this.getAdmreObj().getApplyname() : "");
        StringBuffer content = new StringBuffer();

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat formatT = new SimpleDateFormat("yyyy年M月d日");
        EnumerationHelper helper =  SpringUtil.getBean(EnumerationHelper.class);
        if (CollectionUtil.isNotEmpty(this.getApplicantList())) {
            StringBuffer applyBuffer = new StringBuffer().append("    ");
            this.getApplicantList().stream().forEach(element -> {
                // 公民
                if (element.getType().equals(Tp_applicant.CITIZEN)) {
                    applyBuffer.append("申请人（公民）：").append(element.getName());
                    if (ObjectUtil.isNotNull(element.getGender())) {
                        applyBuffer.append("，" + helper.getDescription(element.getGender()));
                    }
                    if(ObjectUtil.isNotNull(element.getNation())) {
                        applyBuffer.append("，" + helper.getDescription(element.getNation()));
                    }
                    if(StringUtils.isNotEmpty(element.getBirthym())) {
                        try {
                            applyBuffer.append("，" + formatT.format(format.parse(element.getBirthym())));
                        } catch (ParseException e) {
                            e.printStackTrace();
                        }
                    }
                    if(StringUtils.isNotEmpty(element.getAddr())) {
                        applyBuffer.append("，" + element.getAddr());
                    }
                    applyBuffer.append("\n" + "    ");
                }
                // 企业
                if (element.getType().equals(Tp_applicant.UNIT)) {
                    applyBuffer.append("申请人（法人或者其他组织）：").append(element.getName());
                    if(StringUtils.isNotEmpty(element.getAddr())) {
                        applyBuffer.append("，" + element.getAddr());
                    }
                    if(StringUtils.isNotEmpty(element.getManager())) {
                        applyBuffer.append("，" + element.getManager());
                    }
                    if(StringUtils.isNotEmpty(element.getJob())) {
                        applyBuffer.append("，" + element.getJob());
                    }
                    applyBuffer.append("\n" + "    ");
                }
            });
            content.append(applyBuffer);
        }
        // 被申请人
        content.append("被申请人：");
        content.append(this.getAdmreObj().getTargetname());
        content.append("\n");

        String template_content  = TEMPLATE.replace("{apl_admre_audit_targetname}", ObjectUtil.isNotNull(this.getAdmreObj()) ?
                this.getAdmreObj().getTargetname() : "" );
        template_content = template_content.replace("{pronoun}", this.transPronoun());
        template_content = template_content.replace("{behavior}", this.transBehavior());
        template_content = template_content.replace("{prcode}", this.transPrcode());
        template_content = template_content.replace("{apl_main_subtime}", this.transSubtime());

        content.append(template_content);
        template.setContent(content.toString());
        template.setStatement(STATEMENT_TEMPLATE);
        return template;
    }
}
